<?php

!defined('IN_APP') && exit('Access Denied');

class usercontrol extends controlbase {


    function ondefault() {
    	 include template('mycenter');
    }
    
    
    function oncode() {
        $code = random(4, 2);
        #$this('user')->save_code(strtolower($code));
        makecode($code);
    }

    function onregister() {
      	$navtitle = '注册';
		 if (!$this->setting['allow_register']) {
            	$this->message("系统注册功能暂时处于关闭状态!", 'BACK');
       	 }
		if (isset($this->post['submit'])) {
      		$username=$user['username']=$this->post['username'];
      		$user['password']=$this->post['password'];
      		$email=$user['email']=$this->post['email'];
      		$finduser=$this('user')->get_by_username($username);
      		if($finduser) $this->message("用户名 $username 已经注册!", 'c=user&a=register');
      		if (!$this('user')->check_usernamecensor($username)) {
                	$this->message("用户名 $username 被禁止注册!", 'c=user&a=register');
            	}
			if (!$this('user')->check_emailaccess($email)) {
                	$this->message("邮件地址被禁止注册!", 'c=user&a=register');
            	}
      		$uid=$this('user')->add($user);
      		$this->message('恭喜，注册成功！正在转向到登录页面......','c=user&a=login');
        } else {
            include template('register');
        }
    }

    function onlogin() {
        $navtitle = '登录';
        $forward = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : SITE_URL;
        if (isset($this->post['submit'])) {
			$username=$this->post['username'];
			$password=md5($this->post['password']);
			
			$isphone=preg_match('/^[1]([3|5|8][0-9]{1})[0-9]{8}$/', $username);
			if($isphone)$user=$this('user')->get_by_phone($username);
			
			$isemail=preg_match('/^[\w\-\.]+@[\w\-]+(\.\w+)+$/', $username);
			if($isemail)$user=$this('user')->get_by_email($username);
			
			$isphone || $isemail || $user=$this('user')->get_by_username($username);

			if(is_array($user)&&($password==$user['password'])){
				$this('user')->refresh($user);
				$this->message("登录成功!");
			}else{
				$this->message('用户名或密码错误！', 'c=user&a=login');
			}
        }
        include template('login');
    }

    /* 用于ajax登录 */

    function onajaxlogin() {
        exit('-1');
    }

    /* 用于ajax检测用户名是否存在 */

    function onajaxusername() {
		$username = $this->post['username'];
		$user = $this('user')->get_by_username($username);
		if (is_array($user) )exit('-1');
		$usernamecensor = $this('user')->check_usernamecensor($username);
		if (FALSE == $usernamecensor)exit('-2');
		exit('1');
    }

    /* 用于ajax检测用户名是否存在 */

    function onajaxemail() {
        $email = $this->post['email'];
        $user = $this('user')->get_by_email($email);
        if (is_array($user))exit('-1');
        $emailaccess = $this('user')->check_emailaccess($email);
        if (FALSE == $emailaccess)exit('-2');
        exit('1');
    }

    /* 用于ajax检测验证码是否匹配 */

    function onajaxcode() {
        $code = strtolower(trim($this->post['code']));
        echo( intval($code == $this('user')->get_code()) );
    }
    
    /* 用于ajax检测是否超过18岁 */
    function onajaxadult() {
        $btime =strtotime(trim($this->get['bday']));
        $needbtime=strtotime("-18 year"); 
        $isadult=($btime<=$needbtime);
        echo intval($isadult);
    }
    

	
    /* 退出系统 */

    function onlogout() {
        $navtitle = '登出系统';
        $forward = isset($_SERVER['HTTP_REFERER']) ? $_SERVER['HTTP_REFERER'] : SITE_URL;
        $this('user')->logout();
        $this->message('成功退出！');
    }

    /* 找回密码 */

    function ongetpass() {
        $navtitle = '找回密码';
        if (isset($this->post['submit'])) {
            $email = $this->post['email'];
            $name = $this->post['username'];
            $this->checkcode(); //检查验证码
            $touser = $this('user')->get_by_name_email($name, $email);
            if ($touser) {
                $authstr = strcode($touser['username'], $this->setting['auth_key']);
                $this('user')->update_authstr($touser['uid'], $authstr);
                $getpassurl = SITE_URL . '?user/resetpass/' . urlencode($authstr);
                $subject = "找回您在" . $this->setting['site_name'] . "的密码";
                $message = '<p>如果是您在<a swaped="true" target="_blank" href="' . SITE_URL . '">' . $this->setting['site_name'] . '</a>的密码丢失，请点击下面的链接找回：</p><p><a swaped="true" target="_blank" href="' . $getpassurl . '">' . $getpassurl . '</a></p><p>如果直接点击无法打开，请复制链接地址，在新的浏览器窗口里打开。</p>';
                sendmail($touser, $subject, $message);
                $this->message("找回密码的邮件已经发送到你的邮箱，请查收!", 'BACK');
            }
            $this->message("用户名或邮箱填写错误，请核实!", 'BACK');
        }
        include template('getpass');
    }

    /* 重置密码 */

    function onresetpass() {
        $navtitle = '重置密码';
        @$authstr = $this->get[2] ? $this->get[2] : $this->post['authstr'];
        if (empty($authstr)
        )
            $this->message("非法提交，缺少参数!", 'BACK');
        $authstr = urldecode($authstr);
        $username = strcode($authstr, $this->setting['auth_key'], 'DECODE');
        $theuser = $this('user')->get_by_username($username);
        if (!$theuser || ($authstr != $theuser['authstr']))
            $this->message("本网址已过期，请重新使用找回密码的功能!", 'BACK');
        if (isset($this->post['submit'])) {
            $password = $this->post['password'];
            $repassword = $this->post['repassword'];
            if (strlen($password) < 6) {
                $this->message("密码长度不能少于6位!", 'BACK');
            }
            if ($password != $repassword) {
                $this->message("两次密码输入不一致!", 'BACK');
            }
            $this('user')->uppass($theuser['uid'], $password);
            $this('user')->update_authstr($theuser['uid'], '');
            $this->message("重置密码成功，请使用新密码登录!");
        }
        include template('resetpass');
    }


   
    /* 个人中心修改资料 */

    function onprofile() {
        $navtitle = '个人资料';
        if (isset($this->post['submit'])) {
		$user = $this->post;
		unset($user['submit']);
		$uid=$this->user['uid'];
		$this('user')->update($uid,$user);//保存个人信息
        	$this->message("个人资料更新成功", 'c=user&a=profile');
        }
        $fieldlist=$this('userfield')->findAll(array('state'=>1));
        include template('profile');
    }

	/* 补充个人资料 */
	function onsupply(){
        $navtitle = '补充个人资料';
        $id=intval($this->get['id']);
        if($this('userevent')->get_one($id,$this->user['uid'])){
			$this->message("您已经报名过此活动", 'c=user&a=event');
        }
        if (isset($this->post['submit'])) {
			$user = $this->post;
			unset($user['submit']);
			$uid=$this->user['uid'];
            	$this('user')->update($uid,$user);//保存个人信息
			$eventid=intval($id);
			$this('userevent')->add($eventid,$uid,$this->user['username']);
			$this('event')->inc($eventid,'follownum');
            	$this->message("报名成功", 'c=user&a=event');
        }
		$fieldlist=$this('userfield')->findAll(array('state'=>1));
        include template('supply');
	}
	
	
    function onuppass() {
        $navtitle = "修改密码";
        if (isset($this->post['submit'])) {
        	$paras='c=user&a=uppass';
        	
        	$newpwd=trim($this->post['newpwd']);
        	$confirmpwd=trim($this->post['confirmpwd']);
        	$oldpwd=trim($this->post['oldpwd']);
            
            !$newpwd && $message='新密码不能为空，请输入新密码！';
            strlen($newpwd)<6 && $message='密码长度为6～16位的字符串，请重新输入！';
		$newpwd != $confirmpwd && $message='两次输入的密码不一致，请重新设置！';
            $oldpwd == $newpwd && $message='新密码和当前密码不能相同，请重新设置!';
            md5($oldpwd) != $this->user['password'] && $message='旧密码错误!';
     	isset($message) && $this->message($message, $paras);
            
            $this('user')->uppass($this->user['uid'], $newpwd);
            $this->message("密码修改成功!", 'c=user&a=login');
             
        }
        include template('uppass');
    }

 

    function onupimg() {
    	include template('upimg');
    }

    function onevent() {
    	$eventids=$this('userevent')->get_event_ids(
    		array('uid'=>$this->user['uid'])
    	);
    	$eventlist=$this('event')->get_list(
    		array('id'=> array('$in'=>$eventids))
    	);
	     include template('myevent');
    }



}

?>